//
//  AbstractRequest.m
//  collection
//
//  Created by zhangjing on 4/3/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "AbstractRequest.h"

@implementation RequestData

- (void)dealloc {
    
    self.sessionKey = nil;
    
    [super dealloc];
}

@end

@implementation Response

- (void)dealloc {
    
    self.message = nil;
    
    [super dealloc];
}

@end

@implementation AbstractRequest

- (id)init {
    
    self = [super init];
    if (self) {
        [self initParams];
    }
    
    return self;
}

- (void)dealloc {
    
    self.descriptionName = nil;
    self.request = nil;
    self.response = nil;
    
    [super dealloc];
}

- (void)initParams {
    
     // Need implementation by sub class
}

- (NSString*)buildRequestURL {
    
    // Need implementation by sub class
    
    return nil;
}

- (void)parseResponse:(NSObject*)data {
   
    // Need implementation by sub class
}

- (void)postRequest {
    
    NSString* requestPart = [self buildRequestURL];
    
    NSString* url = [NSString stringWithFormat:@"http://localhost/weibo/gateway.php?api=%@%@", self.descriptionName, requestPart];
    XLOG("Req : %@", url);
    
    [self startRequest:url postData:nil];
}

- (void)parserDataInThread:(NSData *)recvData {
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
    NSString* str = [[NSString alloc] initWithData:recvData encoding:enc];
    NSDictionary* jsonValue = [str JSONValue];
    
    NSString* value =  nil;
    
    // status
    value = [jsonValue objectForKey:@"status"];
    if (value != nil || ![value isEqualToString:@""]) {
        self.response.status = value.intValue;
    }
    else {
        NSLog(@"Status error");
        return;
    }
    
    // msg
    value = [jsonValue objectForKey:@"msg"];
    if (value != nil) {
        self.response.message = value;
    }
    
    // data
    if (self.response.status == 0) {
        NSObject* data = [jsonValue objectForKey:@"data"];
        [self parseResponse:data];
    }
    else {
        // To do : 
    }
    
    [str release];
}

- (UserInfo*)parseOneUser:(NSDictionary*)userData {
    
    UserInfo* newUser = [[UserInfo alloc] init];
    
    NSString* value = nil;
    
    // user ID
    value = [userData objectForKey:@"id"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.userID = value.longLongValue;
    }
    
    // nick name
    value = [userData objectForKey:@"nick"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.nickname = value;
    }
    
    // avatar
    value = [userData objectForKey:@"avagar"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.avatarURL = value;
    }
    
    // signature
    value = [userData objectForKey:@"profile"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.signature = value;
    }
    
    // feed num
    value = [userData objectForKey:@"noteNum"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.feedNum = value.intValue;
    }
    
    // follower num
    value = [userData objectForKey:@"followerNum"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.followerNum = value.longLongValue;
    }
    
    // followee num
    value = [userData objectForKey:@"followeeNum"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.followeeNum = value.longLongValue;
    }
    
    // favorite num
    value = [userData objectForKey:@"favorateNum"];
    if (value != nil && ![value isEqualToString:@""]) {
        newUser.favouriteNum = value.intValue;
    }
    
    return newUser;
}

- (Feed*)parseOneFeed:(NSDictionary *)feedData {
    
    Feed* newFeed = [[Feed alloc] init];
    
    NSString* value = nil;
    
    // FID
    value = [feedData objectForKey:@"id"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.feedID = value.longLongValue;
    }
    
    // UID
    value = [feedData objectForKey:@"uid"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.userID = value.longLongValue;
    }
    
    // type
    value = [feedData objectForKey:@"type"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.feedType = value.intValue;
    }
    
    // nickname
    value = [feedData objectForKey:@"nick"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.nickname = value;
    }
    
    // avatar
    value = [feedData objectForKey:@"avatar"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.avatarURL = value;
    }
    
    // note
    value = [feedData objectForKey:@"note"];
    if (value != nil && ![value isEqualToString:@""]) {
        newFeed.text = value;
    }
    
    // pics
    
    // tags
    
    // timestamp
    
    // favNum
    
    // cmNum
    
    // reply, To do : 可能有递归存在
    
    return newFeed;
}

@end
